package com.demo.codeGenerator;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成代码的工具
 * @author liangqingqing
 * @date 2017年11月16日	
 *
 */
public class CodeGeneratorUtil {

    private static final String KONGGE = " ";

    //通过list存储
    /**
     * 字段名称
     */
     private static List<String> fieldNameList = new ArrayList<>();
    /**
     * 字段类型
     */
     private static List<String> dataTypeList = new ArrayList<>();
    /**
     * 字段注释
     */
     private static List<String> commentList = new ArrayList<>();
    /**
     * 是否必填
     */
     private static List<String> isNecessaryList = new ArrayList<>();
    /**
     * 备注
     */
     private static List<String> remarkArrList = new ArrayList<>();

    /**
     * 根据模板excel文件生成文件
     * @param excelPath
     * @throws Exception
     */
    public static void generateByTemplateExcel (String excelPath)throws Exception{
        FileInputStream fileStream = new FileInputStream( new File(excelPath) ) ;
        Workbook workBook = new XSSFWorkbook( fileStream ) ;
        Sheet sheet = workBook.getSheetAt(0);
        int lastrow= sheet.getLastRowNum();

        Row row ;
        for(int j=1;j<=lastrow;j++){
            row = sheet.getRow(j);
            fieldNameList.add(row.getCell(0).getStringCellValue());
            dataTypeList.add(row.getCell(1).getStringCellValue());
            commentList.add(row.getCell(2).getStringCellValue());
            isNecessaryList.add(row.getCell(3).getStringCellValue());
            remarkArrList.add(row.getCell(4).getStringCellValue());
        }

        generateByList(fieldNameList,dataTypeList,commentList,isNecessaryList,remarkArrList);
    }


    public static void generateByList(List<String> fieldNameList,List<String> dataTypeList,List<String> commentList,List<String> isNecessaryList,List<String> remarkArrList){
        for(int i=0;i<fieldNameList.size();i++){
            String dataType  = getRealDateType(dataTypeList.get(i));
            String fieldName = fieldNameList.get(i);
            //注释里的内容：
            String temp = commentList.get(i) + "；是否必填：" + isNecessaryList.get(i) + "；备注：" + remarkArrList.get(i);
            String comment = getRealComment(temp);

            String ret =  comment + "private" + KONGGE + dataType + KONGGE + fieldName + ";";
            System.out.println(ret);
        }


    }
    /**
     * 根据List生成代码
     * @throws Exception
     */
    @Test
    public static void generateByList() throws Exception{

        List<String> fieldNameList = getList("C:/Users/sh00952/Desktop/interfaceDoc/fieldName.txt");
        List<String> dataTypeList = getList("C:/Users/sh00952/Desktop/interfaceDoc/dataType.txt");
        List<String> commentList = getList("C:/Users/sh00952/Desktop/interfaceDoc/comment.txt");


        generateByList(fieldNameList,dataTypeList,commentList,null,null);
    }

    
    /**
     * 获取文档中一列的list
     * @return
     */
    public static List<String> getList(String filePath) throws Exception{
    	List<String> list = new ArrayList<>();
    	File file = new File(filePath);
    	Reader r = new FileReader(file);
    	BufferedReader br  =new BufferedReader(r);
    	String line ;
    	while((line = br.readLine() )!= null){
    		list.add(line);
    	}
    	br.close();
    	return list;
    }


    /**
     * 翻译数据类型
     * @param inputStr
     * @return
     */
    public static String getRealDateType(String inputStr){
        String ret = "Object" ;
        if(inputStr == null){
            return ret ;
        }
        inputStr = inputStr.toLowerCase();

        if(inputStr.contains("string")){
            ret = "String";
        }
        if(inputStr.contains("integer")){
            ret = "Integer";
        }
        if(inputStr.contains("decimal")){
            ret = "Double";
        }
        if(inputStr.contains("date")){
            ret = "Date";
        }
        if(inputStr.contains("double")){
            ret = "Double";
        }

        return ret;
    }



    /**
     * 添加注释
     * @param inputStr
     * @return
     */
    public static String getRealComment(String inputStr){
        String ret = "";

        String l1 = "/**\n";
        String l2 = " *";
        String l3 = " */\n";
        ret  = ret + l1 + l2 + KONGGE + inputStr + "\n" + l3;

        return ret;
    }

}
